package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.HotAreaAdapter;
import com.hemaapp.yjnh.bean.HotDistrict;

/**首页-田家-更多热门区域
 * @author GuiyouZhang
 * Date：2015年12月31日 上午10:06:52
 * Email:guiyouzhang@163.com
 */
public class MoreHotAreaActivity extends BaseActivity {

	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private RefreshLoadmoreLayout refreshLayout;
	private XtomListView listview;

	private int page;
	private ArrayList<HotDistrict> hotDistricts = new ArrayList<HotDistrict>();//热门区域
	private HotAreaAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_more_area);
		super.onCreate(savedInstanceState);
		getHotAreaList();
	}
	
	private void getHotAreaList() {
		getNetWorker().hotDistrictList("4", "", page+"");
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case HOTDISTRICT_LIST:
			showProgressDialog("请稍候");
			break;
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case HOTDISTRICT_LIST:
			cancelProgressDialog();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case HOTDISTRICT_LIST:
			String page = netTask.getParams().get("page");
			HemaPageArrayResult<HotDistrict> dResult = (HemaPageArrayResult<HotDistrict>) baseResult;
			if (dResult == null || dResult.getObjects() == null)
				return;
			ArrayList<HotDistrict> areas = dResult.getObjects();
			if ("0".equals(page)) {// 刷新
				refreshLayout.refreshSuccess();
				hotDistricts.clear();
				hotDistricts.addAll(areas);
			}else {
				refreshLayout.loadmoreSuccess();
				if (areas.size() > 0)
					this.hotDistricts.addAll(areas);
				else {
					refreshLayout.setLoadmoreable(false);
					XtomToastUtil.showShortToast(mContext, "已经到最后啦");
				}
			}
			refreshDatas(-1);
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case HOTDISTRICT_LIST:
			showTextDialog(baseResult.getMsg());
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case HOTDISTRICT_LIST:
			refreshDatas(failedType);
			break;
		default:
			break;
		}
	}
	
	private void refreshDatas(int failedType) {
		if (adapter == null) {
			adapter = new HotAreaAdapter(mContext, hotDistricts, 2);
			adapter.setEmptyString("暂无更多地区");
			listview.setAdapter(adapter);
		}else {
			adapter.setEmptyString("暂无更多地区");
			adapter.notifyDataSetChanged();
		}
		adapter.setFailtype(failedType);
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);

		refreshLayout = (RefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
		listview = (XtomListView) findViewById(R.id.listview);
	}

	@Override
	protected void getExras() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		titleText.setText("更多热门区域");
		titleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleRight.setVisibility(View.GONE);
		refreshLayout.setOnStartListener(new StartListener());
	}

	private class StartListener implements OnStartListener {

		@Override
		public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
			page = 0;
			getHotAreaList();
		}

		@Override
		public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
			page++;
			getHotAreaList();
		}
	}
}
